<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <title>邮箱与提醒配置</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="icon" href="../../static/images/favicon.ico" th:href="@{/images/favicon.ico}"/>
    <link rel="stylesheet" href="../../static/lib/admin/lib/layui-v2.6.3/css/layui.css" th:href="@{/lib/admin/lib/layui-v2.6.3/css/layui.css}" media="all">
    <link rel="stylesheet" href="../../static/lib/admin/css/public.css" th:href="@{/lib/admin/css/public.css}" media="all">
    <style>
        .layui-form-item .layui-input-company {width: auto;padding-right: 10px;line-height: 38px;}
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="layui-form layuimini-form" lay-filter="data-form">
            <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                <legend>发信邮箱设置</legend>
            </fieldset>


            <div class="layui-form-item">
                <label class="layui-form-label">发信开关-默认开</label>
                <div class="layui-input-block">
                    <input type="checkbox" checked="" name="status" lay-skin="switch" lay-filter="switchTest" lay-text="ON|OFF">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">发信模式</label>
                <div class="layui-input-block" style="width: 50%;">
                    <select name="mailCloud" lay-filter="mailCloud" lay-verify="">
                        <option value="">请选择</option>
                        <option value="1">SMTP发信</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">SMTP服务器</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input name="mailSmtp" th:value="${mailSmtp}" lay-verify="mailSmtp" class="layui-input">
                    <tip>SMTP服务器地址以及端口查询<a href="https://blog.jun-mou.top/blogs/45" target="_blank">点击这里</a></tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">SMTP端口</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input type="text" name="mailPort" th:value="${mailPort}" lay-verify="mailPort" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发信人</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input type="text" name="mailUser" th:value="${mailUser}" class="layui-input">
                    <tip>邮件发信抬头</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发信账号</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input type="text" name="mailName" th:value="${mailName}" lay-verify="email" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱密码</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input type="" name="mailPwd" th:value="${mailPwd}" class="layui-input">
                    <tip>不是邮箱密码，是邮箱的授权码，可参考<a href="https://zhuanlan.zhihu.com/p/551399559" target="_blank">点击查看</a></tip>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">测试发信接收邮箱</label>
                <div class="layui-input-block" style="width: 50%;">
                    <input type="" id="demoMail" name="demoMail" class="layui-input">
                    <tip>非必填，仅测试使用</tip>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn layui-btn-primary" lay-submit lay-filter="setEmailDemo">测试邮件发送</button>
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn layui-btn-normal" lay-submit lay-filter="setEmail">保存</button>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.2/dist/jquery.min.js"></script><!--引入jquery js-->
<script src="../../static/lib/admin/lib/layui-v2.6.3/layui.js" th:src="@{/lib/admin/lib/layui-v2.6.3/layui.js}" charset="utf-8"></script>

<script th:inline="javascript">
    layui.use(['form'], function(){
        var form = layui.form
        $ = layui.$;

        form.val('data-form',{
            'mailCloud': [[${mailCloud}]],
            'status': [[${status}]]
        });
    });

</script>

<script>
    layui.use(['form'], function () {
        var form = layui.form
            , layer = layui.layer;

        // form.on('switch(switchTest)', function(data){

            // var status = (this.checked ? 1 : 0);
            //
            // $.ajax({
            //     type: "post",
            //     url: '../page/updateSendEmailStatus',
            //     data: {"status":status},
            //     success: function (data) {
            //         if (data.code == 200) {
            //             layer.msg(contexts+'成功',{icon: 1, time: 2000,});
            //         }else{
            //             layer.msg(data.msg,{icon: 5, time: 2000,});
            //         }
            //     }
            // });
            // layer.msg('开关checked：'+ (this.checked ? 'true' : 'false'), {
            //     offset: '6px'
            // });
            //
            // layer.msg(data.othis)
            // layer.tips('温馨提示：请注意开关状态的文字可以随意定义，而不仅仅是ON|OFF', data.othis)
        // });

        //发信测试
        form.on('submit(setEmailDemo)', function (data) {

            var pattEmail = new RegExp("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
            if (data.field.demoMail == '' || data.field.demoMail == null || !pattEmail.test(data.field.demoMail)){
                layer.tips('请正确输入邮箱', '#demoMail', {
                    tips: [1, '#0FA6D8']
                });
                return false;
            }

            $.ajax({
                url: '../page/setEmailDemo',
                type: 'post',
                data: {"demoMail":data.field.demoMail},
                success: function (suc) {
                    if (suc.code == 200) {
                        layer.msg(suc.msg, {icon: 1});
                    } else {
                        layer.msg(suc.msg, {icon: 5});
                    }
                },
                'error': function () {
                    layer.msg('系统错误');
                }
            });

            return false;
        });


        //监听提交 保存
        form.on('submit(setEmail)', function (data) {

            data.field.status = data.field.status ? 1 : 0;

            $.ajax({
                url: '../page/saveEmail',
                type: 'post',
                dataType: 'json',
                contentType:'application/json;charset=utf-8',
                data: JSON.stringify(data.field),
                success: function (suc) {
                    if (suc.code == 200) {
                        layer.msg(suc.msg, {icon: 1});
                    } else {
                        layer.msg(suc.msg, {icon: 5});
                    }
                },
                'error': function () {
                    layer.msg('系统错误');
                }
            });

            return false;
        });

    });
</script>
</body>
</html>